Added button to toggle visibility of disabled agents (#1464)

Added button to toggle visibility of disabled agents

* Moved index code to separate file

* Added cookie to allow toggling visibility of disabled agents

* Filter disabled agents based on cookie

* Removed js and use correct cookie name

* CR changes

George Opritescu 8 ans auparavant
Parent
Commettre
ad4a15d6da

+ 0 - 1
app/assets/javascripts/pages/agent-show-page.js.coffee

@@ -66,4 +66,3 @@ class @AgentShowPage
66 66
 
67 67
 $ ->
68 68
   Utils.registerPage(AgentShowPage, forPathsMatching: /^agents\/\d+/)
69
-  

+ 30 - 0
app/controllers/agents_controller.rb

@@ -8,12 +8,26 @@ class AgentsController < ApplicationController
8 8
 
9 9
     @agents = current_user.agents.preload(:scenarios, :controllers).reorder(table_sort).page(params[:page])
10 10
 
11
+    if show_only_enabled_agents?
12
+      @agents = @agents.where(disabled: false)
13
+    end
14
+
11 15
     respond_to do |format|
12 16
       format.html
13 17
       format.json { render json: @agents }
14 18
     end
15 19
   end
16 20
 
21
+  def toggle_visibility
22
+    if show_only_enabled_agents?
23
+      mark_all_agents_viewable
24
+    else
25
+      set_only_enabled_agents_as_viewable
26
+    end
27
+
28
+    redirect_to agents_path
29
+  end
30
+
17 31
   def handle_details_post
18 32
     @agent = current_user.agents.find(params[:id])
19 33
     if @agent.respond_to?(:handle_details_post)
@@ -257,4 +271,20 @@ class AgentsController < ApplicationController
257 271
       @agent = FormConfigurableAgentPresenter.new(@agent, view_context)
258 272
     end
259 273
   end
274
+
275
+  private
276
+  def show_only_enabled_agents?
277
+    !!cookies[:huginn_view_only_enabled_agents]
278
+  end
279
+
280
+  def set_only_enabled_agents_as_viewable
281
+    cookies[:huginn_view_only_enabled_agents] = {
282
+      value: "true",
283
+      expires: 1.year.from_now
284
+    }
285
+  end
286
+
287
+  def mark_all_agents_viewable
288
+    cookies.delete(:huginn_view_only_enabled_agents)
289
+  end
260 290
 end

+ 8 - 0
app/helpers/agent_helper.rb

@@ -6,6 +6,14 @@ module AgentHelper
6 6
     end
7 7
   end
8 8
 
9
+  def toggle_disabled_text
10
+    if cookies[:huginn_view_only_enabled_agents]
11
+      " Show Disabled Agents"
12
+    else
13
+      " Hide Disabled Agents"
14
+    end
15
+  end
16
+
9 17
   def scenario_links(agent)
10 18
     agent.scenarios.map { |scenario|
11 19
       link_to(scenario.name, scenario, class: "label", style: style_colors(scenario))

+ 1 - 0
app/views/agents/index.html.erb

@@ -13,6 +13,7 @@
13 13
         <%= link_to icon_tag('glyphicon-plus') + ' New Agent', new_agent_path, class: "btn btn-default" %>
14 14
         <%= link_to icon_tag('glyphicon-refresh') + ' Run event propagation', propagate_agents_path, method: 'post', class: "btn btn-default" %>
15 15
         <%= link_to icon_tag('glyphicon-random') + ' View diagram', diagram_path, class: "btn btn-default" %>
16
+        <%= link_to icon_tag('glyphicon-adjust') + toggle_disabled_text, toggle_visibility_agents_path, method: :put, class: "btn btn-default visibility-enabler" %>
16 17
       </div>
17 18
     </div>
18 19
   </div>

+ 1 - 0
config/routes.rb

@@ -10,6 +10,7 @@ Huginn::Application.routes.draw do
10 10
     end
11 11
 
12 12
     collection do
13
+      put :toggle_visibility
13 14
       post :propagate
14 15
       get :type_details
15 16
       post :dry_run

+ 23 - 0
spec/controllers/agents_controller_spec.rb

@@ -16,6 +16,14 @@ describe AgentsController do
16 16
       get :index
17 17
       expect(assigns(:agents).all? {|i| expect(i.user).to eq(users(:bob)) }).to be_truthy
18 18
     end
19
+
20
+    it "should not show disabled agents if the cookie is set" do
21
+      @request.cookies["huginn_view_only_enabled_agents"] = "true"
22
+
23
+      sign_in users(:bob)
24
+      get :index
25
+      expect(assigns(:agents).map(&:disabled).uniq).to eq([false])
26
+    end
19 27
   end
20 28
 
21 29
   describe "POST handle_details_post" do
@@ -67,6 +75,21 @@ describe AgentsController do
67 75
     end
68 76
   end
69 77
 
78
+  describe "PUT toggle_visibility" do
79
+    it "should set the cookie" do
80
+      sign_in users(:jane)
81
+      put :toggle_visibility
82
+      expect(response.cookies["huginn_view_only_enabled_agents"]).to eq("true")
83
+    end
84
+
85
+    it "should delete the cookie" do
86
+      @request.cookies["huginn_view_only_enabled_agents"] = "true"
87
+      sign_in users(:jane)
88
+      put :toggle_visibility
89
+      expect(response.cookies["huginn_view_only_enabled_agents"]).to be_nil
90
+    end
91
+  end
92
+
70 93
   describe "POST propagate" do
71 94
     before(:each) do
72 95
       sign_in users(:bob)

+ 22 - 0
spec/features/toggle_visibility_of_disabled_agents.rb

@@ -0,0 +1,22 @@
1
+require 'capybara_helper'
2
+
3
+describe "Toggling the visibility of an agent", js: true do
4
+  it "hides them if they are disabled" do
5
+    login_as(users(:bob))
6
+    visit("/agents")
7
+
8
+    expect {
9
+      click_on("Show/Hide Disabled Agents")
10
+    }.to change{ find_all(".table-striped tr").count }.by(-1)
11
+  end
12
+
13
+  it "shows them when they are hidden" do
14
+    login_as(users(:bob))
15
+    visit("/agents")
16
+    click_on("Show/Hide Disabled Agents")
17
+
18
+    expect {
19
+      click_on("Show/Hide Disabled Agents")
20
+    }.to change{ find_all(".table-striped tr").count }.by(1)
21
+  end
22
+end

+ 20 - 1
spec/fixtures/agents.yml

@@ -32,6 +32,25 @@ bob_website_agent:
32 32
                  }
33 33
                }.to_json.inspect %>
34 34
 
35
+bob_disabled_website_agent:
36
+  type: Agents::WebsiteAgent
37
+  disabled: true
38
+  user: bob
39
+  events_count: 1
40
+  schedule: "midnight"
41
+  name: "Disabled ZKCD"
42
+  guid: <%= SecureRandom.hex %>
43
+  options: <%= {
44
+                 :url => "http://xkcd.com",
45
+                 :expected_update_period_in_days => 2,
46
+                 :mode => :on_change,
47
+                 :extract => {
48
+                   :url => {:css => "#comic img", :value => "@src"},
49
+                   :title => {:css => "#comic img", :value => "@title"}
50
+                 }
51
+               }.to_json.inspect %>
52
+
53
+
35 54
 bob_weather_agent:
36 55
   type: Agents::WeatherAgent
37 56
   user: bob
@@ -130,7 +149,7 @@ jane_basecamp_agent:
130 149
 bob_data_output_agent:
131 150
   type: Agents::DataOutputAgent
132 151
   user: bob
133
-  name: RSS Feed 
152
+  name: RSS Feed
134 153
   guid: <%= SecureRandom.hex %>
135 154
   options: <%= {
136 155
     expected_receive_period_in_days: 3,